using Common;


IConfiguration configuration = new ConfigurationBuilder()
                            .AddJsonFile("appsettings.json")
                            .Build();

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.


builder.Services.AddSingleton(new AppHelper(configuration));

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddHealthChecks();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();
//
var port = 5000;

if (Tools.PortInUse(port))
{
    for (int i = 0; i < 1000; i++)
    {
        port++;
        if (!Tools.PortInUse(port))
            break;
    }
}



configuration.RegToConsul(port);

app.UseHealthChecks("/health");

app.Urls.Add("http://*:" + port);

 
AppDomain.CurrentDomain.ProcessExit += (s, c) =>
{
    configuration.DereConsul(port);
};

app.Run();
